package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.Workstation;
import com.qidianit.mes.pro.mapper.WorkstationMapper;
import com.qidianit.mes.pro.service.WorkstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: YKYB
 * @Date: 2025/10/1 15:04
 * @Description:
 */
@Service
public class WorkstationServiceImpl implements WorkstationService {

    @Autowired
    private WorkstationMapper workstationMapper;

    @Override
    public PageInfo<Workstation> selectList(Workstation workstation,
                                            Integer pageNum,
                                            Integer pageSize) {
        if (workstation == null) {
            workstation = new Workstation();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Workstation> list = workstationMapper.selectList(workstation);
        return new PageInfo<>(list);
    }

    @Override
    public Result<Workstation> getByCode(String code) {
        if (code == null || code.isEmpty()) {
            return Result.error("工作站编码不能为空");
        }
        Workstation workstation = workstationMapper.selectByCode(code);
        if (workstation == null) {
            return Result.error("工作站不存在");
        }
        return Result.success(workstation);
    }

    @Override
    public Result<Workstation> add(Workstation workstation) {
        //校验
        if (workstation.getCode() == null || workstation.getCode().isEmpty()) {
            return Result.error("工作站编码不能为空");
        }
        workstation.setStatus(1);  //启用
        workstation.setDeleteFlag(0);//未删除

        int rows = workstationMapper.insertWorkstation(workstation);
        if (rows <= 0) {
            return Result.error("新增工作站失败");
        }
        return Result.success(workstation);
    }

    @Override
    public Result<Workstation> update(Workstation workstation) {
        if (workstation.getId() == null) {
            return Result.error("缺少主键id");
        }
        int rows = workstationMapper.updateById(workstation);
        if (rows <= 0) {
            return Result.error("更新失败，记录不存在或已删除");
        }
        // 查询最新数据返回
        Workstation latest = workstationMapper.selectById(workstation.getId());
        return Result.success(latest);
    }

    @Override
    public Result<Workstation> deleteById(Long id) {
        if (id == null) {
            return Result.error("缺少主键id");
        }
        Workstation ws = workstationMapper.selectById(id);
        if (ws == null) {
            return Result.error("记录不存在或已删除");
        }
        // 逻辑删除
        int rows = workstationMapper.updateDeleteFlagById(id, 1);
        return rows > 0 ? Result.success() : Result.error("删除失败");
    }

    @Override
    public Result<Void> batchDelete(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Result.error("待删除id列表不能为空");
        }
        int rows = workstationMapper.batchDelete(ids, 1); // 1=删除
        return rows > 0 ? Result.success() : Result.error("批量删除失败");
    }

    @Override
    public Result<Workstation> changeStatus(Long id, Integer status) {
        if (id == null || status == null) {
            return Result.error("参数缺失");
        }
        int rows = workstationMapper.updateStatusById(id, status);
        if (rows <= 0) {
            return Result.error("更新状态失败，记录不存在或已删除");
        }
        return Result.success(workstationMapper.selectById(id));
    }
    @Override
    public Result<List<Workstation>> getByWorkshopId(Long workshopId) {
        if (workshopId == null) {
            return Result.error("车间ID不能为空");
        }
        List<Workstation> list = workstationMapper.selectByWorkshopId(workshopId);
        return Result.success(list);
    }
}
